using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The UserAccountTypeCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about UserAccountTypeCodeTable objects.
/// </summary>
public class UserAccountTypeCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of UserAccountTypeCodeTable from the underlying datasource.
/// </summary>
/// <param name="userAccountTypeCode">The unique UserAccountTypeCode of the UserAccountTypeCodeTable in the database.</param>
/// <returns>An UserAccountTypeCodeTable when the UserAccountTypeCode was found in the database, or null otherwise.</returns>
public static UserAccountTypeCodeTable GetItem(short userAccountTypeCode)
{
UserAccountTypeCodeTable myUserAccountTypeCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTypeCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountTypeCode", userAccountTypeCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myUserAccountTypeCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myUserAccountTypeCodeTable;
}
}

/// <summary>
/// Returns a list with UserAccountTypeCodeTable objects.
/// </summary>
/// <returns>A generics List with the UserAccountTypeCodeTable objects.</returns>
public static UserAccountTypeCodeTableList GetList()
{
UserAccountTypeCodeTableList tempList = new UserAccountTypeCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTypeCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a UserAccountTypeCodeTable in the database.
/// </summary>
/// <param name="myUserAccountTypeCodeTable">The UserAccountTypeCodeTable instance to save.</param>
/// <returns>The new UserAccountTypeCode if the UserAccountTypeCodeTable is new in the database or the existing UserAccountTypeCode when an item was updated.</returns>
public static Int16 Save(UserAccountTypeCodeTable myUserAccountTypeCodeTable)
{
Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTypeCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myUserAccountTypeCodeTable.UserAccountTypeCode == -1)
{myCommand.Parameters.AddWithValue("@userAccountTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountTypeCode", myUserAccountTypeCodeTable.UserAccountTypeCode);
}
if (string.IsNullOrEmpty(myUserAccountTypeCodeTable.UserAccountTypeName))
{
myCommand.Parameters.AddWithValue("@userAccountTypeName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountTypeName", myUserAccountTypeCodeTable.UserAccountTypeName);
}
if (string.IsNullOrEmpty(myUserAccountTypeCodeTable.Description))
{
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myUserAccountTypeCodeTable.Description);
}
if (myUserAccountTypeCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myUserAccountTypeCodeTable.Status);
}
if (myUserAccountTypeCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myUserAccountTypeCodeTable.CreatedDateTime);
}
if (myUserAccountTypeCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myUserAccountTypeCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a UserAccountTypeCodeTable from the database.
/// </summary>
/// <param name="userAccountTypeCode">The UserAccountTypeCode of the UserAccountTypeCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(short userAccountTypeCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAccountTypeCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@userAccountTypeCode", userAccountTypeCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the UserAccountTypeCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static UserAccountTypeCodeTable FillDataRecord(IDataRecord myDataRecord )
{
UserAccountTypeCodeTable myUserAccountTypeCodeTable = new UserAccountTypeCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountTypeCode")))
{
myUserAccountTypeCodeTable.UserAccountTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("UserAccountTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountTypeName")))
{
myUserAccountTypeCodeTable.UserAccountTypeName = myDataRecord.GetString(myDataRecord.GetOrdinal("UserAccountTypeName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myUserAccountTypeCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myUserAccountTypeCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myUserAccountTypeCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myUserAccountTypeCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myUserAccountTypeCodeTable;
}
}

 } 
